package org.jeecg.modules.building.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.config.JeecgBaseConfig;
import org.jeecg.modules.building.entity.TbBuilding;
import org.jeecg.modules.building.service.ITbBuildingService;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Description: 房屋明细表
 */
@Api(tags = "房屋明细表")
@RestController
@RequestMapping("/building/tbBuilding")
@Slf4j
public class TbBuildingController extends JeecgController<TbBuilding, ITbBuildingService> {
    @Autowired
    private ITbBuildingService tbBuildingService;

    /**
     * 分页列表查询
     *
     * @param tbBuilding
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "房屋明细表-分页列表查询")
    @ApiOperation(value = "房屋明细表-分页列表查询", notes = "房屋明细表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<TbBuilding>> queryPageList(TbBuilding tbBuilding,
                                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                   HttpServletRequest req) {
        List<TbBuilding> statistics = tbBuildingService.statistics(tbBuilding, req.getParameterMap());
        IPage<TbBuilding> page = new Page(pageNo, pageSize);
        page.setTotal(statistics.size());
        int startIndex = (pageNo - 1) * pageSize; // 起始索引
        int endIndex = Math.min(startIndex + pageSize, statistics.size());
        if (startIndex >= endIndex) {
            return Result.OK(page);
        }
        page.setRecords(statistics.subList(startIndex, endIndex));
        return Result.OK(page);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param tbBuilding
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, TbBuilding tbBuilding) {
        return this.exportXls(tbBuilding, request, TbBuilding.class, "房屋明细表");
    }

    @Resource
    private JeecgBaseConfig jeecgBaseConfig;

    protected ModelAndView exportXls(TbBuilding tbBuilding, HttpServletRequest request, Class clazz, String title) {
        // Step.2 获取导出数据
        List<TbBuilding> exportList = tbBuildingService.statistics(tbBuilding, request.getParameterMap());
        // Step.3 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        //此处设置的filename无效 ,前端会重更新设置一下
        mv.addObject(NormalExcelConstants.FILE_NAME, title);
        mv.addObject(NormalExcelConstants.CLASS, clazz);
        //update-begin--Author:liusq  Date:20210126 for：图片导出报错，ImageBasePath未设置--------------------
        ExportParams exportParams = new ExportParams("（2024）年度市国资委出资企业固定资产-房屋明细表（汇总）", "单位：平方米、元、套、栋", title);
        exportParams.setImageBasePath(jeecgBaseConfig.getPath().getUpload());
        //update-end--Author:liusq  Date:20210126 for：图片导出报错，ImageBasePath未设置----------------------
        mv.addObject(NormalExcelConstants.PARAMS, exportParams);
        mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
        return mv;
    }

}
